package com.groot.medical.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.MedicalInstitution;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.service.MedicalService;
import com.groot.medical.service.MedicalServiceImpl;
@WebServlet("/sys/medicalI.do")
public class MedicalInstitutionController extends BaseServletController {
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	private MedicalService medicalService = new MedicalServiceImpl();
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 分页查询医疗机构列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findMedicalByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 5;
		//枫叶信息
		PageBean menuPage = medicalService.findByPage(pageNum, pageSize);
		System.out.println("menuPage:"+menuPage);
		request.setAttribute("menuPage", menuPage);
		return "/WEB-INF/page/medical/medical_list.jsp";
	}
	/**
	 * 打开添加医疗机构页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openAddMedical(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		return "/WEB-INF/page/medical/medical_add.jsp";
	}
	/**
	 * 添加医疗机构
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addMedical(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String medicalid = request.getParameter("medicalid");
		String medicalname = request.getParameter("medicalname");
		String medicaladdress = request.getParameter("medicaladdress");
		String tgId = request.getParameter("tgId");
		String pageNum = request.getParameter("pageNum");
		
		MedicalInstitution medical = new MedicalInstitution();
		medical.setMedicalId(medicalid);
		medical.setMedicalName(medicalname);
		medical.setMedicalAddress(medicaladdress);
		int i=medicalService.addMedical(medical);
		if(i==1){
			request.setAttribute("msg", "<script>alert('修改成功')</script>");
			return findMedicalByPage(request,response);
		
		}else
			LOGGER.info("添加失败"+i);
		
		return null;
	}
		/**
		 * 打开修改医疗机构信息页面
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public String openEditMedical (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String id = request.getParameter("id");
			MedicalInstitution medical=medicalService.findMedicalInfoById(id);
			request.setAttribute("tgId",request.getParameter("tgId"));
			request.setAttribute("medical",medical);
			return "/WEB-INF/page/medical/medical_edit.jsp";
		}
		/**
		 * 修改医疗机构信息
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public String editMedical (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int id = Integer.valueOf(request.getParameter("id"));
			String medicalid = request.getParameter("medicalid");
			String medicalname = request.getParameter("medicalname");
			String medicaladdress = request.getParameter("medicaladdress");
			String tgId = request.getParameter("tgId");
			String pageNum = request.getParameter("pageNum");
			MedicalInstitution medical = new MedicalInstitution();
			medical.setId(id);
			medical.setMedicalId(medicalid);
			medical.setMedicalName(medicalname);
			medical.setMedicalAddress(medicaladdress);
			
			int i=medicalService.editMedical(medical);
			if(i==1){
				request.setAttribute("msg", "<script>alert('修改成功')</script>");
				return findMedicalByPage(request,response);
			
			}else
				LOGGER.info("添加失败"+i);
			return null;
		}
		/**
		 * 异步查询医疗机构名称是否存在
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public void CheckNameExist (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String medicalname = request.getParameter("medicalname");
			int i=medicalService.CheckNameExist(medicalname);
			System.out.println("爽："+i);
			String resultString = "true";
	        if (i!=0 ){
	            resultString = "false";
	        }
			response.setContentType("text/html");
	        ServletOutputStream outputStream = response.getOutputStream();
	        outputStream.write(resultString.getBytes());
	        outputStream.close();
		}
	
}
